package crm.view.Custom;

import crm.dao.CustomerDao;
import crm.dao.impl.CustomerSqlSeverImpl;
import crm.entity.Customer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

/**
 * 客户查看自己的基本信息
 */
public class LookBasicCustom implements ActionListener {
    JFrame frame = null;
    JLabel id = null;
    JLabel name = null;
    JLabel pwd = null;
    JLabel phone = null;
    JLabel address = null;
    JLabel credit = null;
    JLabel products = null;
    JLabel time = null;
    JLabel contactId = null;
    JTextField IdField = null;
    JTextField NameField = null;
    JTextField PwdField = null;
    JTextField PhoneField = null;
    JTextField AddressField = null;
    JTextField CreditField = null;
    JTextField ProductionField = null;
    JTextField TimeField = null;
    JTextField ContactIdField = null;

    JButton btn = null;
    JButton btn2 = null;


    public static void main(String[] args) {
        new LookBasicCustom();
    }

    public LookBasicCustom() {
        init();
        frame.setBounds(680, 200, 600, 700);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLayout(null);
        frame.setVisible(true);
    }

    public void init() {
        frame = new JFrame("客户基本信息");
        id = new JLabel("用户编号:");
        name = new JLabel("输入姓名:");
        pwd = new JLabel("用户密码:");
        phone = new JLabel("联系方式:");
        address = new JLabel("联系地址:");
        credit = new JLabel("信用度:");
        products = new JLabel("所购产品:");
        time = new JLabel("消费时间:");
        contactId = new JLabel("联系人编号:");
        IdField = new JTextField();
        NameField = new JTextField();
        PwdField = new JTextField();
        PhoneField = new JTextField();
        AddressField = new JTextField();
        CreditField = new JTextField();
        ProductionField = new JTextField();
        TimeField = new JTextField();
        ContactIdField = new JTextField();

        // 通过用户name查询
        btn = new JButton("查询");
        frame.add(btn);
        btn2 = new JButton("清空");
        frame.add(btn2);

        Font font = new Font("宋体", 1, 20);
        btn.setFont(font);
        btn2.setFont(font);
        id.setFont(font);
        IdField.setFont(font);
        name.setFont(font);
        NameField.setFont(font);
        pwd.setFont(font);
        PwdField.setFont(font);
        phone.setFont(font);
        PhoneField.setFont(font);
        address.setFont(font);
        AddressField.setFont(font);
        credit.setFont(font);
        CreditField.setFont(font);
        products.setFont(font);
        ProductionField.setFont(font);
        contactId.setFont(font);
        ContactIdField.setFont(font);
        time.setFont(font);
        TimeField.setFont(font);


        frame.add(id);
        frame.add(IdField);
        frame.add(name);
        frame.add(NameField);
        frame.add(pwd);
        frame.add(PwdField);
        frame.add(phone);
        frame.add(PhoneField);
        frame.add(address);
        frame.add(AddressField);
        frame.add(credit);
        frame.add(CreditField);
        frame.add(products);
        frame.add(ProductionField);
        frame.add(time);
        frame.add(TimeField);
        frame.add(contactId);
        frame.add(ContactIdField);

        name.setBounds(40, 40, 100, 60);
        NameField.setBounds(150, 50, 300, 40);

        btn.setBounds(200, 100, 80, 30);
        btn2.setBounds(330, 100, 80, 30);



        IdField.setEditable(false);

        pwd.setBounds(40, 140, 100, 60);
        PwdField.setBounds(150, 150, 300, 40);
        PwdField.setEditable(false);

        phone.setBounds(40, 190, 100, 60);
        PhoneField.setBounds(150, 200, 300, 40);
        PhoneField.setEditable(false);

        address.setBounds(40, 240, 100, 60);
        AddressField.setBounds(150, 250, 300, 40);
        AddressField.setEditable(false);

        credit.setBounds(40, 290, 100, 60);
        CreditField.setBounds(150, 300, 300, 40);
        CreditField.setEditable(false);

        products.setBounds(40, 340, 100, 60);
        ProductionField.setBounds(150, 350, 300, 40);
        ProductionField.setEditable(false);

        time.setBounds(40, 390, 100, 60);
        TimeField.setBounds(150, 400, 300, 40);
        TimeField.setEditable(false);

        contactId.setBounds(40, 440, 140, 60);
        ContactIdField.setBounds(150, 450, 300, 40);
        ContactIdField.setEditable(false);

        id.setBounds(40,490,140,60);
        IdField.setBounds(150,500,300,40);
        btn.addActionListener(this);
        btn2.addActionListener(this);

    }


    @Override
    public void actionPerformed(ActionEvent e) {
        CustomerDao dao = new CustomerSqlSeverImpl();
        Customer customer = new Customer();
        String name = NameField.getText();
        if ("查询".equals(e.getActionCommand())) {
            if ("".equals(NameField.getText())){
                JOptionPane.showMessageDialog(null, "请输入用户姓名进行查询！");
            }else{
                List<Customer> getname = dao.getByName(name);
                for (Customer customer1 : getname) {
                    IdField.setText(String.valueOf(customer1.getCustomer_id()));
                    PwdField.setText(customer1.getCustomer_pwd());
                    PhoneField.setText(customer1.getCustomer_phone());
                    ProductionField.setText(customer1.getProducts());
                    AddressField.setText(customer1.getCustomer_address());
                    CreditField.setText(String.valueOf(customer1.getCredit()));
                    TimeField.setText(String.valueOf(customer1.getConsume_time()));
                    ContactIdField.setText(customer1.getContact_id());

                }

            }
        }
        else if ("清空".equals(e.getActionCommand())){
            IdField.setText("");
            NameField.setText("");
            PwdField.setText("");
            PhoneField.setText("");
            AddressField.setText("");
            CreditField.setText("");
            ProductionField.setText("");
            TimeField.setText("");
            ContactIdField.setText("");

        }
    }
}
